#!/bin/sh

e2e_test_install() {
  NODE_LABEL_KEY="$(random_string)"
  NODE_LABEL_VALUE="$(random_string)"

  kubectl create namespace "$CLUSTER_NAMESPACE"
  PGCBOUNCER="pgbouncer"

  cat <<EOF | kubectl apply -f -
apiVersion: stackgres.io/v1
kind: SGInstanceProfile
metadata:
  name: size-s
  namespace: $CLUSTER_NAMESPACE
spec:
  cpu: '500m'
  memory: '512Mi'
EOF

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGPoolingConfig
metadata:
  name: $PGCBOUNCER
  namespace: $CLUSTER_NAMESPACE
spec:
  pgBouncer:
    pgbouncer.ini:
      pgbouncer:
        max_client_conn: '2000'
        default_pool_size: '50'
      databases:
        foodb:
          max_db_connections: 1000
          pool_size: 20
          dbname: 'bardb'
          reserve_pool: 5
          datestyle: 'ISO'
      users:
        user1:
          pool_mode: transaction
          max_user_connections: 50
        user2:
          pool_mode: session
          max_user_connections: '100'
EOF

  cat << EOF | kubectl create -f -
apiVersion: stackgres.io/v1
kind: SGCluster
metadata:
  name: $CLUSTER_NAME
  namespace: $CLUSTER_NAMESPACE
spec:
  instances: 1
  postgres:
    version: 'latest'
  configurations:
    sgPoolingConfig: $PGCBOUNCER
  sgInstanceProfile: 'size-s'
  pods:
    persistentVolume:
      size: '32Mi'
    scheduling:
      nodeSelector:
        $NODE_LABEL_KEY: $NODE_LABEL_VALUE"
  nonProductionOptions:
    disableClusterPodAntiAffinity: true
EOF

  wait_until has_cluster_generated_resources
}

e2e_test() {
  run_test "max_client_conn should appear in pgbouncer configmap only once" check_pgbouncer_max_client_conn

  run_test "default_pool_size should appear in pgbouncer configmap only once" check_pgbouncer_default_pool_size

  run_test "databases section should appear in pgbouncer configmap" check_pgbouncer_database

  run_test "users section should appear in pgbouncer configmap" check_pgbouncer_users

  run_test "default parameters should appear in pgbouncer configmap" check_pgbouncer_defaults
}

check_pgbouncer_max_client_conn(){
  check_parameter_count "max_db_connections" 2
}

check_pgbouncer_default_pool_size(){
  check_parameter_count "default_pool_size" 1
}

check_pgbouncer_database(){
  check_pgbouncer_data "foodb" "dbname=bardb"
  check_pgbouncer_data "foodb" "max_db_connections=1000"
  check_pgbouncer_data "foodb" "reserve_pool=5"
  check_pgbouncer_data "foodb" "datestyle=ISO"
  check_pgbouncer_data "*" "port=5432"
}

check_pgbouncer_users(){
  check_pgbouncer_data "user1" "pool_mode=transaction"
  check_pgbouncer_data "user1" "max_user_connections=50"
  check_pgbouncer_data "user2" "pool_mode=session"
  check_pgbouncer_data "user2" "max_user_connections=100"
}

check_pgbouncer_defaults(){
  check_pgbouncer_data "auth_user" "authenticator"
  check_pgbouncer_data "auth_type" "md5"
  check_pgbouncer_data "ignore_startup_parameters" "extra_float_digits"
  check_pgbouncer_data "application_name_add_host" "1"
  check_pgbouncer_data "listen_port" "6432"
  check_pgbouncer_data "unix_socket_dir" "/var/run/postgresql"
}

check_pgbouncer_data(){
  get_pgbouncer_configmap | grep "$1 = " | grep "$2"
  if [ $? -eq 0 ]; then
    success "$2 is appearing for the $1"
  else
    fail "$2 is not appearing"
  fi
}

check_parameter_count(){
  PARAM="$1"
  COUNT="$2"
  PARAM_COUNT="$(get_pgbouncer_configmap | grep -c "$PARAM")"

  if assert_string_equal "$COUNT" "$PARAM_COUNT"
  then
    success "$PARAM is appearing only $COUNT time(s)"
  else
    fail "$PARAM is appearing more than once"
  fi
}

get_pgbouncer_configmap() {
  kubectl get cm -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-connection-pooling-config" -o json | jq -r '.data."pgbouncer.ini"'
}
